package cn.stylefeng.guns.sys.modular.tao.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.tao.entity.GoodsPic;
import cn.stylefeng.guns.sys.modular.tao.model.params.GoodsPicParam;
import cn.stylefeng.guns.sys.modular.tao.service.GoodsPicService;
import cn.stylefeng.guns.sys.modular.tao.service.GoodsService;
import cn.stylefeng.guns.sys.modular.tao.shiUtil.ResponseMsg;
import cn.stylefeng.guns.sys.modular.tao.shiUtil.UploadFileUtils;
import cn.stylefeng.guns.sys.modular.tao.shiUtil.UploadResult;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.util.Base64;


/**
 * 商品图片控制器
 *
 * @author 
 * @Date 2019-08-22 14:16:25
 */
@Controller
@RequestMapping("/goodsPic")
public class GoodsPicController extends BaseController {

    private static final String SystemConstant = "https://hybn-1259399233.cos.ap-chengdu.myqcloud.com/";

    String imagesduo = "";


    private String PREFIX = "/goodsPic";

    @Autowired
    private GoodsPicService goodsPicService;

    @Autowired
    private GoodsService goodsService;
    /**
     * 跳转到主页面
     *
     * @author 
     * @Date 2019-08-22
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/goodsPic.html";
    }

    /**
     * 新增页面
     *
     * @author 
     * @Date 2019-08-22
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/goodsPic_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 
     * @Date 2019-08-22
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/goodsPic_edit.html";
    }

    /**
     * 修改接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(GoodsPicParam goodsPicParam, String Details, HttpSession session) {
        byte[] decoded = Base64.getDecoder().decode(Details);
        //修改商品价格
        if(goodsPicParam.getPrice() == ""){
            goodsPicParam.setPrice("0.00");
        }
        int num = goodsService.getgoodsById(goodsPicParam.getGoodsId());
        if(num == 0){
            goodsService.addPrice(goodsPicParam.getGoodsId(),goodsPicParam.getPrice());
        }else{
            goodsService.updateById(goodsPicParam.getGoodsId(),goodsPicParam.getPrice());
        }

        String decodeStr = new String(decoded);
        System.out.println(decodeStr);
        goodsPicParam.setGoodsDetails(decodeStr);
        String images = (String)session.getAttribute("images");
        goodsPicParam.setGoodsPic(images);

        GoodsPic pic = goodsPicService.getByGoodsId(goodsPicParam.getGoodsId());
        if(pic==null && goodsPicParam.getGoodsPic()!=null && !"".equals(goodsPicParam.getGoodsPic())){
            //如果该商品在图片表没有图片信息 且 上传了路径
            goodsPicParam.setGoodsPics(imagesduo);
            this.goodsPicService.add(goodsPicParam);
            imagesduo = "";
            return ResponseData.success();
        }else if(pic!=null){
            goodsPicParam.getGoodsPics();
            this.goodsPicService.updateByGoodsId(goodsPicParam);
            imagesduo = "";
            return ResponseData.success();
        }else{
            return ResponseData.success();
        }
    }

    /**
     * 编辑接口
     *
     * @author 
     * @Date 2019-08-22
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(GoodsPicParam goodsPicParam) {
        this.goodsPicService.update(goodsPicParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 
     * @Date 2019-08-22
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(GoodsPicParam goodsPicParam) {
        this.goodsPicService.delete(goodsPicParam);
        return ResponseData.success();
    }



    /**
     * 查看详情接口
     *
     * @author 
     * @Date 2019-08-22
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(GoodsPicParam goodsPicParam, HttpSession session) {
        GoodsPic detail = this.goodsPicService.getById(goodsPicParam.getId());
        session.setAttribute("goodsPicParamId",detail.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 
     * @Date 2019-08-22
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(GoodsPicParam goodsPicParam) {
        imagesduo = "";
        return this.goodsPicService.findPageBySpec(goodsPicParam);
    }






    /**
     * 单图片上传接口
     */
    @RequestMapping(method = RequestMethod.POST, path = "/UploadFileUtils")
    @ResponseBody
    public ResponseData UploadFileUtils(@RequestPart("file") MultipartFile file, HttpSession session) throws FileNotFoundException {
        if (ToolUtil.isEmpty(file)){
            return  ResponseData.error(201,"上传失败，图片为空");
        }else{
            ResponseMsg msg = UploadFileUtils.uploadFile(file);
            if (msg.getStatus() == 10001){
                UploadResult result = (UploadResult) msg.getResult();
                session.setAttribute("images",SystemConstant+result.getPath());
                return  ResponseData.success(200,"上传成功",result.getPath());
            }else {
                return  ResponseData.error(202,"上传失败，图片格式错误");
            }
        }
    }



    /**
     * 多图片上传接口
     */
    @RequestMapping(method = RequestMethod.POST, path = "/multipleGraph")
    @ResponseBody
    public ResponseData multipleGraph(@RequestPart("file") MultipartFile file, HttpSession session) throws FileNotFoundException {

        if (ToolUtil.isEmpty(file)){
            return  ResponseData.error(201,"上传失败，图片为空");
        }else{
            ResponseMsg msg = UploadFileUtils.uploadFile(file);
            if (msg.getStatus() == 10001){
                UploadResult result = (UploadResult) msg.getResult();
                imagesduo+=SystemConstant+result.getPath()+",";
                return  ResponseData.success(200,"上传成功",imagesduo);
            }else {
                return  ResponseData.error(202,"上传失败，图片格式错误");
            }
        }
    }




    /**
     * 删除图片接口
     */
    @RequestMapping(method = RequestMethod.POST, path = "/updateGoodsPic")
    @ResponseBody
    public ResponseData updateGoodsPic(GoodsPicParam goodsPicParam ,HttpSession session, @RequestParam(value = "img", required = false) String img) {
        goodsPicParam.setId(Long.parseLong(session.getAttribute("goodsPicParamId").toString()));
        goodsPicParam.setGoodsPics(img);
        goodsPicService.updateGoodsPic(goodsPicParam);
        return ResponseData.success("删除成功");
    }













}


